import 'package:flutter/material.dart';
import 'package:flutter_ui/common/app_theme.dart';
import 'package:flutter_ui/widgets/dialogs/filter_dialog.dart';
import 'package:flutter_ui/widgets/patient_list_item.dart';
import 'package:flutter_ui/widgets/ai_assistant.dart';

/// 患者列表页面
class PatientListPage extends StatefulWidget {
  const PatientListPage({super.key});

  @override
  State<PatientListPage> createState() => _PatientListPageState();
}

class _PatientListPageState extends State<PatientListPage>
    with SingleTickerProviderStateMixin {
  // 搜索框控制器
  late AnimationController _searchController;
  late Animation<double> _searchAnimation;
  final TextEditingController _searchTextController = TextEditingController();
  bool _isSearching = false;

  // 患者类型选择
  bool _isMyPatients = true;

  // 筛选弹窗控制
  bool _showFilterDialog = false;

  @override
  void initState() {
    super.initState();
    _searchController = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    );
    _searchAnimation = CurvedAnimation(
      parent: _searchController,
      curve: Curves.easeInOut,
    );
  }

  @override
  void dispose() {
    _searchController.dispose();
    _searchTextController.dispose();
    super.dispose();
  }

  // 切换搜索状态
  void _toggleSearch() {
    setState(() {
      _isSearching = !_isSearching;
      if (_isSearching) {
        _searchController.forward();
      } else {
        _searchController.reverse();
        _searchTextController.clear();
      }
    });
  }

  // 显示筛选选项
  void _showFilterOptions() {
    if (_showFilterDialog) {
      _closeFilterDialog();
    } else {
      setState(() {
        _showFilterDialog = true;
      });
    }
  }

  // 关闭筛选对话框
  void _closeFilterDialog() {
    setState(() {
      _showFilterDialog = false;
    });
  }

  // 切换患者类型
  void _togglePatientType(bool isMyPatients) {
    setState(() {
      _isMyPatients = isMyPatients;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        flexibleSpace: Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              colors: [Color(0xFF1E88E5), Color(0xFF0099FF)],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
          ),
        ),
        title:
            _isSearching
                ? AnimatedBuilder(
                  animation: _searchAnimation,
                  builder: (context, child) {
                    return SizedBox(
                      width:
                          MediaQuery.of(context).size.width *
                          0.7 *
                          _searchAnimation.value,
                      child: TextField(
                        controller: _searchTextController,
                        decoration: const InputDecoration(
                          hintText: '请输入床位、患者姓名、住院号',
                          hintStyle: TextStyle(
                            color: Colors.white70,
                            fontSize: 14,
                          ),
                          border: InputBorder.none,
                          isDense: true,
                        ),
                        style: const TextStyle(color: Colors.white),
                        cursorColor: Colors.white,
                      ),
                    );
                  },
                )
                : const Text('心血管内科二病区 | 妙护士'),
        centerTitle: true,
        actions: [
          IconButton(
            onPressed: _toggleSearch,
            icon: Icon(_isSearching ? Icons.close : Icons.search),
          ),
          IconButton(
            onPressed: _showFilterOptions,
            icon: const Icon(Icons.filter_list),
          ),
        ],
      ),
      body: Stack(
        children: [
          Column(
            children: [
              _buildPatientTypeSelector(),
              // _buildPatientFilter(),
              Expanded(
                child: ListView(
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  children: [
                    PatientListItem(
                      bed: '08床',
                      name: '王一博',
                      gender: '男',
                      age: '28岁',
                      id: '202503300090077',
                      color: Colors.blue,
                      hasChild: true,
                      diagnosis: '冠状动脉粥样硬化心脏病',
                      doctorNurse: '郝医生/妙护士',
                      admissionTime: '2025-03-30 09:00 (3天)',
                    ),
                    PatientListItem(
                      bed: '09床',
                      name: '王一博',
                      gender: '男',
                      age: '28岁',
                      id: '202503300090080',
                      color: Colors.blue,
                    ),
                    PatientListItem(
                      bed: '10床',
                      name: '李敏敏',
                      gender: '女',
                      age: '26岁',
                      id: '202503300090081',
                      color: Colors.pink,
                      hasChild: true,
                    ),
                    PatientListItem(
                      bed: '11床',
                      name: '王一博',
                      gender: '男',
                      age: '28岁',
                      id: '202503300090095',
                      color: Colors.blue,
                    ),
                    PatientListItem(
                      bed: '12床',
                      name: '苏小夏',
                      gender: '女',
                      age: '24岁',
                      id: '202503300090081',
                      color: Colors.pink,
                    ),
                    PatientListItem(
                      bed: '20床',
                      name: '赵美美',
                      gender: '女',
                      age: '23岁',
                      id: '202503300090002',
                      color: Colors.pink,
                    ),
                  ],
                ),
              ),
            ],
          ),
          // 筛选弹出框
          if (_showFilterDialog)
            FilterDialog(
              onClose: _closeFilterDialog,
              onConfirm: _closeFilterDialog,
            ),
          // 全局悬浮AI助手
          const AiAssistant(),
        ],
      ),
    );
  }

  /// 构建患者类型选择器
  Widget _buildPatientTypeSelector() {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      color: Colors.white,
      child: Row(
        children: [
          InkWell(
            onTap: () => _togglePatientType(true),
            child: Column(
              children: [
                Text(
                  '我的患者',
                  style: TextStyle(
                    fontWeight:
                        _isMyPatients ? FontWeight.bold : FontWeight.normal,
                    color: _isMyPatients ? AppTheme.primaryColor : Colors.grey,
                  ),
                ),
                const SizedBox(height: 4),
                Container(
                  height: 2,
                  width: 60,
                  color:
                      _isMyPatients
                          ? AppTheme.primaryColor
                          : Colors.transparent,
                ),
              ],
            ),
          ),
          const SizedBox(width: 24),
          InkWell(
            onTap: () => _togglePatientType(false),
            child: Column(
              children: [
                Text(
                  '科室患者',
                  style: TextStyle(
                    fontWeight:
                        !_isMyPatients ? FontWeight.bold : FontWeight.normal,
                    color: !_isMyPatients ? AppTheme.primaryColor : Colors.grey,
                  ),
                ),
                const SizedBox(height: 4),
                Container(
                  height: 2,
                  width: 60,
                  color:
                      !_isMyPatients
                          ? AppTheme.primaryColor
                          : Colors.transparent,
                ),
              ],
            ),
          ),
          const Spacer(),
          Row(
            children: const [
              Text('按床位排序', style: TextStyle(fontSize: 12)),
              Icon(Icons.arrow_drop_down, size: 16),
            ],
          ),
        ],
      ),
    );
  }

  // 移除旧的_buildPatientItem和_buildTag方法，已被PatientListItem组件替代
}
